using System;
using ProtoModels;
using UnityEngine;

public class WalletManager
{
	public delegate void OnSave();

	private OnSave onSave;

	private PlayerModel model;

	public WalletManager(PlayerModel model, OnSave onSave)
	{
		this.model = model;
		this.onSave = onSave;
	}

	public void ReloadModel(PlayerModel model)
	{
		this.model = model;
	}

	public int CoinsForLike()
	{
		return ConfigBase.instance.coinsForLike;
	}

	public int FreeCoins()
	{
		return ConfigBase.instance.freeCoins;
	}

	public CurrencyType FreeCoinsCurrencyType()
	{
		return ConfigBase.instance.freeCoinsCurrencyType;
	}

	public void CollectFreeCoins()
	{
		ReceiveCurrency(FreeCoinsCurrencyType(), FreeCoins());
		model.timeWhenLastCollectedFreeCoins = DateTime.UtcNow.Ticks;
		model.timesCollectedFreeCoins++;
		DoOnSave();
	}

	private void DoOnSave()
	{
		if (onSave != null)
		{
			onSave();
		}
	}

	public DailyPrizesConfig.Prize CurrentPrize()
	{
		CheckDailyCoinsReset();
		return DailyPrizesConfig.instance.PrizeForConsecutiveDays(model.DailyCoinsCount);
	}

	public DailyPrizesConfig.Prize NextPrize()
	{
		CheckDailyCoinsReset();
		return DailyPrizesConfig.instance.PrizeForConsecutiveDays(model.DailyCoinsCount + 1);
	}

	public void CollectDailyCoins()
	{
		if (isDailyCoinsActive())
		{
			int prize = CurrentPrize().prize;
			AddCurrency(CurrencyType.coins, prize);
			model.DailyCoinsCount++;
			DateTime dateTime = DateTime.Today.AddHours(12.0);
			if (dateTime < DateTime.Now)
			{
				dateTime.AddDays(1.0);
			}
			TimeSpan value = new TimeSpan(DailyPrizesConfig.instance.minTimeHours, 0, 0);
			DateTime dateTime2 = DateTime.Now.Add(value);
			model.timeToNextDailyCoins = ((!(dateTime >= dateTime2)) ? dateTime2.Ticks : dateTime.Ticks);
			DoOnSave();
		}
	}

	public TimeSpan DurationForFreeCoins()
	{
		return new TimeSpan(0, 240, 0);
	}

	public bool isNextCoinsActive()
	{
		return DateTime.UtcNow >= new DateTime(model.timeWhenLastCollectedFreeCoins) + DurationForFreeCoins();
	}

	private void CheckDailyCoinsReset()
	{
		if (isDayPassed() && model.DailyCoinsCount != 0)
		{
			model.DailyCoinsCount = 0;
			DoOnSave();
		}
	}

	public bool isDailyCoinsActive()
	{
		CheckDailyCoinsReset();
		return DateTime.Now >= new DateTime(model.timeToNextDailyCoins);
	}

	public TimeSpan TimeTillNextCoinsActive()
	{
		if (isNextCoinsActive())
		{
			return new TimeSpan(0L);
		}
		return (new DateTime(model.timeWhenLastCollectedFreeCoins) + DurationForFreeCoins()).Subtract(DateTime.UtcNow);
	}

	public TimeSpan TimeTillDailyCoinsActive()
	{
		if (isDailyCoinsActive())
		{
			return new TimeSpan(0L);
		}
		return TimeSinceDailyCoinsActive();
	}

	private TimeSpan TimeSinceDailyCoinsActive()
	{
		return new DateTime(model.timeToNextDailyCoins).Subtract(DateTime.Now);
	}

	private bool isDayPassed()
	{
		float num = (float)TimeSinceDailyCoinsActive().TotalHours;
		return num <= -24f;
	}

	public void BuyItem(SingleCurrencyPrice price)
	{
		BuyItem(price.cost, price.currency);
	}

	public void BuyItem(int price, CurrencyType currencyType)
	{
		switch (currencyType)
		{
		case CurrencyType.coins:
			BuyItemCoins(price);
			break;
		case CurrencyType.diamonds:
			model.diamonds = Mathf.Max(0, model.diamonds - price);
			DoOnSave();
			break;
		default:
			BuyItemDollars(price);
			break;
		}
	}

	public void BuyItemCoins(int price)
	{
		model.coins = Mathf.Max(0, model.coins - price);
		DoOnSave();
	}

	public void BuyItemDollars(int price)
	{
		model.giraffeDollars = Mathf.Max(0, model.giraffeDollars - price);
		DoOnSave();
	}

	public void ReceiveCurrency(CurrencyType type, int ammount)
	{
		AddCurrency(type, ammount);
	}

	public bool CurrencyHasMax(CurrencyType type)
	{
		return true;
	}

	public int MaxCurrencyCount(CurrencyType type)
	{
		switch (type)
		{
		case CurrencyType.diamonds:
			return ConfigBase.instance.tokensCap;
		case CurrencyType.coins:
			return ConfigBase.instance.coinsCap;
		case CurrencyType.ggdollars:
			return ConfigBase.instance.ggDollarsCap;
		default:
			return 0;
		}
	}

	public void AddCurrency(CurrencyType type, int ammount)
	{
		switch (type)
		{
		case CurrencyType.coins:
			model.coins += ammount;
			if (CurrencyHasMax(type))
			{
				model.coins = Mathf.Min(MaxCurrencyCount(type), model.coins);
			}
			break;
		case CurrencyType.diamonds:
			model.diamonds += ammount;
			if (CurrencyHasMax(type))
			{
				model.diamonds = Mathf.Min(MaxCurrencyCount(type), model.diamonds);
			}
			break;
		case CurrencyType.ggdollars:
			model.giraffeDollars += ammount;
			if (CurrencyHasMax(type))
			{
				model.giraffeDollars = Mathf.Min(MaxCurrencyCount(type), model.giraffeDollars);
			}
			break;
		}
		DoOnSave();
	}

	public int CurrencyCount(CurrencyType type)
	{
		switch (type)
		{
		case CurrencyType.coins:
			return model.coins;
		case CurrencyType.diamonds:
			return model.diamonds;
		default:
			return model.giraffeDollars;
		}
	}

	public bool CanBuyItemWithPrice(SingleCurrencyPrice price)
	{
		return CanBuyItemWithPrice(price.cost, price.currency);
	}

	public bool CanBuyItemWithPrice(int price, CurrencyType currencyType)
	{
		if (currencyType == CurrencyType.coins)
		{
			return model.coins >= price;
		}
		return model.giraffeDollars >= price;
	}

	public bool CanBuyItemWithPrice(int priceCoins)
	{
		return CanBuyItemWithPrice(priceCoins, CurrencyType.coins);
	}
}
